// 引入pinia
import {
	defineStore,
} from 'pinia';
import {
	computed,
	reactive,
	ref,
} from 'vue';
// 通过 defineStore 创建的一个 store
// 将商品信息加入购物车
export const useaddToCartListStore = defineStore('addToCartList', () => {
	const cart = reactive(JSON.parse(uni.getStorageSync('cart') || '[]'));
	const addToCartList = (goods) => {
		// console.log(cart);
		const findresult = cart.find((ele) => ele.goods_id === goods.goods_id);
		if (!findresult) {
			// 如果购物车中没有这件商品，则直接 push
			// 将 goods 对象转换为响应式对象
			const reactiveGoods = reactive(goods);
			cart.push(reactiveGoods);
		} else {
			// 如果购物车中有这件商品，则只更新数量即可
			findresult.goods_count++;
			// console.log(cart);
		}
		uni.setStorageSync('cart', JSON.stringify(cart));
	};
	const total = computed(() => {
		return cart.reduce((acc, item) => acc + item.goods_count, 0);
	});
	const checkedcount = computed(() => {
		return cart.reduce((acc, item) => {
			if (item.goods_state) {
				return acc + item.goods_count;
			}
		}, 0);
	});
	const checkedmoney = computed(() => {
		return cart.reduce((acc, item) => {
			if (item.goods_state) {
				return acc + item.goods_price * item.goods_count;
			}
		}, 0);
	});
	return {
		cart,
		addToCartList,
		total,
		checkedcount,
		checkedmoney,
	};
});

// 更新加入购物车的商品状态信息
export const useupdateCartListStore = defineStore('updateCartList', () => {
	const updateCartList = (goods) => {
		const addToCartListStore = useaddToCartListStore();
		const findresult = addToCartListStore.cart.find((ele) => ele.goods_id === goods.goods_id);
		if (findresult) {
			findresult.goods_state = goods.goods_state;
		}
		uni.setStorageSync('cart', JSON.stringify(addToCartListStore.cart));
	};
	return {
		updateCartList,
	};
});

// 更新加入购物车的商品数量信息
export const useupdateCartcountStore = defineStore('updateCartcount', () => {
	const updateCartcount = (goods) => {
		const addToCartListStore = useaddToCartListStore();
		const findresult = addToCartListStore.cart.find((ele) => ele.goods_id === goods.goods_id);
		if (findresult) {
			findresult.goods_count = goods.goods_count;
		}
		uni.setStorageSync('cart', JSON.stringify(addToCartListStore.cart));
	};
	return {
		updateCartcount,
	};
});

// 删除购物车里的商品
export const useremovecartByIdStore = defineStore('removecartById', () => {
	const removecartById = (goods_id) => {
		const addToCartListStore = useaddToCartListStore();
		const index = addToCartListStore.cart.findIndex((ele) => ele.goods_id === goods_id);
		// console.log(index);
		addToCartListStore.cart.splice(index, 1);
		uni.setStorageSync('cart', JSON.stringify(addToCartListStore.cart));
	};
	return {
		removecartById,
	};
});

// 更新所有商品勾选状态
export const useupdateallgoodsstateStore = defineStore('updateallgoodsstate', () => {
	const updateallgoodsstate = (state) => {
		const addToCartListStore = useaddToCartListStore();
		addToCartListStore.cart.forEach((ele) => {
			ele.goods_state = state;
		});
		uni.setStorageSync('cart', JSON.stringify(addToCartListStore.cart));
	};
	return {
		updateallgoodsstate,
	};
});
